﻿using System;
using System.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class EclassAdmin
    {
        #region DeleteEclass删除一个班级
        /// <summary>
        /// 删除一个班级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="eclassId"></param>
        public static void DeleteEclass(int schoolId, int eclassId)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteEclass" + eclassId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);

                Eclass eclass = EclassManager.Get1(session, eclassId, schoolId);
                if (eclass == null)
                    throw new Exception("不存在这个班级或者这个班级不属于你这个学校！");
                EclassManager.Delete(session, eclass);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion
        #region UpdateEclass修改一个班级
        /// <summary>
        /// 修改一个班级
        /// </summary>
        /// <param name="eclassId"></param>
        /// <param name="schoolId"></param>
        /// <param name="gradeId"></param>
        /// <param name="leaderId"></param>
        /// <param name="status"></param>
        /// <param name="eclassName"></param>
        public static void UpdateEclass(int eclassId, int schoolId, int gradeId, int leaderId,int status, string eclassName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteEclass"+eclassId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                Basic.Model.Entity.Collision collision1 = new Basic.Model.Entity.Collision();
                collision1.itemKey = "DeleteSchool" + schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision1);
                Basic.Model.Entity.Collision collision2 = new Basic.Model.Entity.Collision();
                collision2.itemKey = "DeleteGrade" + gradeId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision2);
                Basic.Model.Entity.Collision collision3 = new Basic.Model.Entity.Collision();
                collision3.itemKey = "DeleteTeacher" + leaderId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision3);

                Eclass eclass = EclassManager.Get1(session, eclassId, schoolId);
                if (eclass == null)
                    throw new Exception("不存在这个班级，或者这个班级不属于你这个学校！");

                if (status == 9)
                {
                    eclass.grade = GradeManager.Get(session, gradeId);
                    if (eclass.grade == null)
                        throw new Exception("不存在这个年级或者这个年级被删除！");
                    eclass.status = 9;
                }
                else if (status == 1)
                {
                    eclass.grade = null;
                    eclass.status = 1;
                }
                else
                    throw new Exception("错误的状态参数！");
                eclass.leader = TeacherManager.Get(session, leaderId);
                if (eclass.leader == null)
                    throw new Exception("不存在这个教师或者这个教师被删除！");
                eclass.eclassName = eclassName;
                EclassManager.Add(session, eclass);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision1);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision2);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision3);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion
        #region AddEclass添加一个班级
        /// <summary>
        /// 添加一个班级
        /// </summary>
        /// <param name="eclassName"></param>
        /// <param name="schoolId"></param>
        /// <param name="gradeId"></param>
        /// <param name="leaderId">班主任Id与Teacher表关联</param>
        public static void AddEclass(string eclassName, int schoolId, int gradeId, int leaderId)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision1 = new Basic.Model.Entity.Collision();
                collision1.itemKey = "DeleteSchool"+schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision1);
                Basic.Model.Entity.Collision collision2 = new Basic.Model.Entity.Collision();
                collision2.itemKey = "DeleteGrade"+gradeId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision2);
                Basic.Model.Entity.Collision collision3 = new Basic.Model.Entity.Collision();
                collision3.itemKey = "DeleteTeacher"+leaderId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision3);

                Eclass eclass = new Eclass();
                eclass.school = SchoolManager.Get(session, schoolId);
                if (eclass.school == null)
                    throw new Exception("这个学校不存在或者已经被删除！");
                eclass.grade = GradeManager.Get(session, gradeId);
                if (eclass.grade == null)
                    throw new Exception("不存在这个年级或者这个年级被删除！");
                eclass.leader = TeacherManager.Get(session, leaderId);
                if (eclass.leader == null)
                    throw new Exception("不存在这个教师或者这个教师被删除！");
                eclass.eclassName = eclassName;
                eclass.createTime = DateTime.Now;
                eclass.status = 9;
                EclassManager.Add(session, eclass);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision1);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision2);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision3);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion
        #region GetsEclass查询所有班级
        /// <summary>
        /// 查询所有班级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static PageResult<Educational.VO.Eclass> GetsEclass(int schoolId, int rowIndex, int rowCount)
        {
            IList<Eclass> entityData = null;
            PageResult<Educational.VO.Eclass> pageResult = new PageResult<Educational.VO.Eclass>();
            pageResult.data = new List<Educational.VO.Eclass>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = EclassManager.Gets1(session, schoolId, rowIndex, rowCount);
                pageResult.totalCount = EclassManager.Gets1Count(session, schoolId);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.Eclass eclass = new Educational.VO.Eclass();
                eclass.eclassId = entityData[i].eclassId;
                eclass.eclassName = entityData[i].eclassName;
                eclass.createTime = Tools.ToDateTimeString(entityData[i].createTime);
                eclass.status = entityData[i].status;
                eclass.leader = new Educational.VO.Teacher();
                if (entityData[i].leader != null)
                {
                    eclass.leader.teacherId = entityData[i].leader.teacherId;
                    eclass.leader.teacherName = entityData[i].leader.teacherName;
                }
                eclass.grade = new Educational.VO.Grade();
                if (entityData[i].grade != null)
                {
                    eclass.grade.gradeId = entityData[i].grade.gradeId;
                    eclass.grade.gradeName = entityData[i].grade.gradeName;
                }
                pageResult.data.Add(eclass);
            }
            return pageResult;
        }
        #endregion
    }
}
